package com.byxy.xyqas.controller;

import javax.servlet.http.HttpServletRequest;
import com.byxy.xyqas.service.ForumService;
import com.byxy.xyqas.service.ReplyService;
import com.byxy.xyqas.service.TabService;
import com.byxy.xyqas.service.TipService;
import com.byxy.xyqas.vo.ForumVO;
import com.byxy.xyqas.vo.TabVO;
import com.byxy.xyqas.vo.TipVO;
import com.byxy.xyqas.vo.UserVO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * TipController
 */
@Controller
public class TipController {

    @Autowired
    private TipService tipService;

    @Autowired
    private ReplyService replyService;

    @Autowired
    private TabService tabService;

    @Autowired
    private ForumService forumService;

    @GetMapping("toMainPage.do")
    public String toMainPage(Model model) {
        model.addAttribute("tips", tipService.findAll());
        return "main";
    }

    @GetMapping("toPublishTip.do")
    public String toPublishTip(Model model) {
        model.addAttribute("forums", forumService.findAll());
        model.addAttribute("tabs", tabService.findAll());
        return "publishTip";
    }

    @GetMapping("showTip.do")
    public String showTip(Model model, @RequestParam("tipId") Integer tipId) {
        model.addAttribute("tip", tipService.get(tipId));
        model.addAttribute("replies", replyService.findAll());
        return "tipContent";
    }

    @RequestMapping("publishNewTip.do")
    public String publishNewTip(@RequestParam("tip_title") String tipTitle,
            @RequestParam("selectedForumId") Integer forumId, @RequestParam("tab_id") Integer tabId,
            @RequestParam("tip_content") String tipContent, Model model, HttpServletRequest request) {
        UserVO userVO = (UserVO) request.getSession().getAttribute("USER");
        TipVO tipVO = new TipVO();
        tipVO.setTipTitle(tipTitle);
        tipVO.setTipContent(tipContent);
        tipVO.setUserId(userVO.getUserId());
        tipVO.setTabId(tabId);
        tipService.add(tipVO);
        return "redirect:toMainPage.do";
    }

    @GetMapping("toTipManagePage.do")
    public String toTipManage(Model model) {
        model.addAttribute("tips", tipService.findAll());
        return "tipManage";
    }

    @GetMapping("toModifyTipPage.do")
    public String toModifyTipPage(Model model, @RequestParam("tipId") Integer tipId) {
        model.addAttribute("forums", forumService.findAll());
        model.addAttribute("tip", tipService.get(tipId));
        model.addAttribute("tabs", tabService.findAll());
        return "modifyTip";
    }

    @RequestMapping("modifyTip.do")
    public String modifyTip(@RequestParam("tip_id") Integer tipId, @RequestParam("selectedForumId") Integer forumId,
            @RequestParam("selectedTabId") Integer tabId, @RequestParam("tip_title") String tipTitle,
            @RequestParam("tip_content") String tipContent) {
        TipVO tipVO = tipService.get(tipId);
        tipVO.setTipContent(tipContent);
        tipVO.setTipTitle(tipTitle);
        TabVO tabVO = tabService.get(tabId);
        tipVO.setTabName(tabVO.getTabName());
        tipService.update(tipVO);
        ForumVO f = forumService.get(forumId);
        tabVO.setForumName(f.getForumName());
        tabService.update(tabVO);
        return "redirect:toTipManagePage.do";
    }

    @GetMapping("changeTipStatus.do")
    public String ChangeTipStatus(@RequestParam("tipId") Integer tipId, @RequestParam("opr") Integer opr) {
        TipVO tipVO = tipService.get(tipId);
        if (opr == 3) {
            tipService.changeTipStatus(tipVO.getTipIsDeleted(), 1, tipId);
        } else if (opr == 1) {
            tipService.changeTipStatus(1, tipVO.getTipIsKnot(), tipId);
        } else if (opr == 2) {
            tipService.changeTipStatus(0, tipVO.getTipIsKnot(), tipId);
        } else if (opr == 4) {
            tipService.changeTipStatus(tipVO.getTipIsDeleted(), 0, tipId);
        }
        return "redirect:toMainPage.do";
    }
}